package com.noah.note.manager;

import android.view.View;
import android.widget.TextView;

import com.noah.note.bean.MsgEvent;
import com.noah.note.global.Const;
import com.noah.note.global.SiteWebsocket;
import com.noah.note.utils.LogUtils;

import org.java_websocket.drafts.Draft_6455;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;

import io.socket.client.Ack;
import io.socket.client.IO;
import io.socket.client.Socket;
import io.socket.emitter.Emitter;

/**
 * Created by zzh on 2018/8/27.
 */

public class SocketManager {
    private static SocketManager instance;

    private SocketManager() {
    }

    public static SocketManager getInstance() {
        if (instance == null) {
            synchronized (SocketManager.class) {
                if (instance == null) {
                    instance = new SocketManager();
                }
            }
        }
        return instance;
    }

    private SiteWebsocket siteWebsocket;
    int type;

    public void testSocket(TextView tvTest) {
        try {
            siteWebsocket = new SiteWebsocket(new URI(Const.BASE_WEB_SOCKET + "?uid=" + LoginManager.getInstance().getUser().getUid()), new Draft_6455(), tvTest);
            siteWebsocket.connectBlocking();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        tvTest.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                switch (type % 10) {
                    case 0:
                        siteWebsocket.send("报告非凡哥，警署有规定嘎， 警员屋企有紧要事，有权请事假嘎。");
                        break;
                    case 1:
                        siteWebsocket.send("警署都有规定！下属唔可以驳上司嘴！");
                        break;
                    case 2:
                        siteWebsocket.send("报告非凡哥，我宜家唔系驳你嘴，我系同你讲道理！");
                        break;
                    case 3:
                        siteWebsocket.send("刘醒！我系你上司！我话你讲嘅唔系道理就系唔系道理！我话你驳嘴就驳嘴！杨阳！叫晒所有人落操场。 （对刘醒） 你要当住所有人面前大声讲 ‘对唔住，非凡哥！’我听唔到你再讲！再听唔到继续讲！讲到我听到为止！");
                        break;
                    case 4:
                        siteWebsocket.send("报告非凡哥！唔使落操场喇！我话宜家喺度讲都得！都系六个字，吔屎啦！梁非凡");
                        break;
                    case 5:
                        siteWebsocket.send("你！");
                        break;
                    case 6:
                        siteWebsocket.send("你听唔到我再讲！再听唔到我继续讲！讲到你听到为止！“吔屎啦！梁非凡！");
                        break;
                    case 7:
                        siteWebsocket.send("我要炒你啊！");
                        break;
                    case 8:
                        siteWebsocket.send("我唔使你炒而系我炒你！我宜家劈炮唔捞啦，你跪喺度翳我返黎我都唔返黎啊！");
                        break;
                    case 9:
                        siteWebsocket.send("???????");
                        break;
                }
                type++;
            }
        });
    }

    private Socket socket;

    public void connectSocket() {
        try {
            if (socket != null && socket.connected()) {
                return;
            }
            IO.Options options = new IO.Options();
            options.reconnectionAttempts = 7;     // 重连尝试次数
            options.reconnectionDelay = 3000;     // 失败重连的时间间隔(ms)
            options.timeout = 10000;              // 连接超时时间(ms)
            options.forceNew = true;
            options.transports = new String[]{"websocket"};
            options.query = "uid=" + LoginManager.getInstance().getUser().getUid();
            socket = IO.socket(Const.BASE_HOST + ":8090/", options);
            socket.on(Socket.EVENT_CONNECT, new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    LogUtils.w("连接成功EVENT_CONNECT");
                    sendMsg("我连接了...");
                }
            }).on(Socket.EVENT_CONNECT_ERROR, new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    LogUtils.w("连接错误CONNECT_ERROR");
                }
            }).on(Socket.EVENT_CONNECT_TIMEOUT, new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    LogUtils.w("连接超时CONNECT_TIMEOUT");
                }
            }).on(Socket.EVENT_PING, new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    LogUtils.w("Socket.EVENT_PING");
                }
            }).on(Socket.EVENT_PONG, new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    LogUtils.w("Socket.EVENT_PONG");
                }
            }).on(Socket.EVENT_DISCONNECT, new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    LogUtils.w("连接断开EVENT_DISCONNECT");
                }
            }).on(Socket.EVENT_MESSAGE, new Emitter.Listener() {
                @Override
                public void call(Object... args) {
                    LogUtils.w("收到:" + Arrays.toString(args));
                }
            });
            socket.connect();
        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }

    }

    private void sendMsg(String message) {
        MsgEvent msgEvent = new MsgEvent();
        LogUtils.i("发送:" + JsonManager.getInstance().toJsonString(msgEvent));
        socket.emit(Socket.EVENT_MESSAGE, JsonManager.getInstance().toJsonString(msgEvent), new Ack() {
            @Override
            public void call(Object... args) {
                LogUtils.i("发送收到:" + Arrays.toString(args));
            }
        });
    }
}
